package com.adv.ems.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.adv.ems.db.entity.BuildingInfoDO;
import com.adv.ems.db.entity.CollectPointDO;
import com.adv.ems.enums.SubjectTypeEnum;
import com.adv.ems.manager.BuildingInfoManager;
import com.adv.ems.manager.CircuitDiagramNodeMeterRelationManager;
import com.adv.ems.manager.CircuitDiagramNodeEnergySubjectRelationManager;
import com.adv.ems.manager.CollectPointManager;
import com.adv.ems.service.EnergyUserRelatePointService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EnergyUserRelatePointServiceImpl implements EnergyUserRelatePointService {
    @Resource
    CircuitDiagramNodeMeterRelationManager circuitDiagramNodeDeviceRelationManager;
    @Resource
    CircuitDiagramNodeEnergySubjectRelationManager circuitDiagramNodeEnergySubjectRelationManager;
    @Resource
    CollectPointManager collectPointManager;
    @Resource
    BuildingInfoManager buildingInfoManager;


    @Override
    public List<CollectPointDO> getPoints(Long subjectId, SubjectTypeEnum subjectType) {
        List<Long> diagramNodeIds = circuitDiagramNodeEnergySubjectRelationManager.getDiagramNodeIds(ListUtil.of(subjectId), subjectType);
        List<Long> meterIds = circuitDiagramNodeDeviceRelationManager.getDeviceIds(diagramNodeIds);
        return collectPointManager.getPoints(meterIds, null);
    }

    @Override
    public List<CollectPointDO> getPoints(Long instituteId) {
        Set<Long> buildingIds = buildingInfoManager.getAllMap().values().stream().filter(b -> instituteId.equals(b.getInstituteId())).map(BuildingInfoDO::getId).collect(Collectors.toSet());
        return buildingIds.stream().flatMap(bid -> getPoints(bid, SubjectTypeEnum.BUILDING).stream()).collect(Collectors.toList());
    }
}
